home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Animation Routines / C - Animation Project.p < prev    next >
Text File  |  1993-03-31  |  10KB  |  267 lines

  1.  {The following program demonstrates most of the routines created in the unit CellusoftAnimations. }
  2. {}
  3. {What does the program do?}
  4. {The application created by this program will display a window of size 300 X 300, with a picture taken from}
  5. {PICT resource ID 130 and animate a bunch of rotating eyeballs, each of which will rotate from one side of the}
  6. {window to the other continuously.  The graphics for the eyeballs are from PICT ID 128 and the masks are from}
  7. {PICT ID 129.  In order to gain fuller understanding of the program, you may wish to look through the resource}
  8. {file used by the program.}
  9. {}
  10. {Animation Project© 1993 Tony Small All Rights Reserved WorldWide}
  11. {}
  12. {Many parts of the program do not directly pertain to routines in CelluSoftAnimations, but all is thoroughly }
  13. {documented to give further insight into graphics, mac design and animation programming.}
  14.  
  15. program Animation_Project;
  16.  
  17.     uses
  18.         CelluSoftAnimations;
  19.  
  20.     const
  21.         MaxEyes = 6;  {This number specifies the number of eyeballs that will roll across the window.}
  22.                       {This number should not be bigger than the MaxAnimations constant in the CellusoftAnimations unit.}
  23.     type
  24.         eyeballs = record
  25.                 currentRect, lastRect, pictRect: rect;  {These rects are required by the animation routines...}
  26.                                                     {currentRect: Where the eyeball is currently on the screen.}
  27.                                                     {lastRect: Where the eyeball was on the screen in the last animation loop.}
  28.                                                     {pictRect: Where the picture of the current eyeball is located in the Eyeball port.}
  29.                 step: integer;                                {Step specifies at what stage the eyeball is in its movement.}
  30.                 goingLeft: boolean;                        {If this is true, then the eyeball is moving left.  If not, then right.}
  31.             end;
  32.  
  33.     var
  34.         MainWindow: WindowPtr;                                {The variable for the main window.}
  35.         PureBack, Eyeball, Eyeballmask, offLoad: GrafPtr;    {The variables for the ports.}
  36.         eyeBallArray: array[1..MaxEyes] of eyeballs;        {The array of eyeball records.}
  37.  
  38.     function Randomize (range: Integer): Integer;                {This function gives a random number from 1 to range.}
  39.         var
  40.             rawResult: Longint;
  41.     begin
  42.         rawResult := Random;
  43.         rawResult := abs(rawResult);
  44.         Randomize := (rawResult * range) div 32768 + 1;
  45.     end;
  46.  
  47.     procedure InitializeEyes;
  48.  
  49. {This procedure initializes all variables of all eyeball records in the eyeballArray.}
  50. {It sets the currentRect for each eyeball at a random rect within the bounds of the window.}
  51. {It sets the step to 1 so each eyeball will start out flat on the screen.}
  52. {It randomly sets the goingleft boolean variable.}
  53. {It sets the pictRect to the area in the eyeball port that holds the flat eyeball picture.}
  54.  
  55.         var
  56.             i: integer;
  57.     begin
  58.         for i := 1 to MaxEyes do        {Do the following for each eyeball.}
  59.             with eyeBallArray[i] do
  60.                 begin
  61.                     currentRect.left := Randomize(300 - 19);
  62.                     currentRect.top := Randomize(300 - 10);
  63.                     currentRect.right := currentRect.left + 19;
  64.                     currentRect.bottom := currentRect.top + 10;
  65.                     lastRect := currentRect;
  66.                     step := 1;
  67.                     if Randomize(2) = 1 then
  68.                         goingLeft := true
  69.                     else
  70.                         goingLeft := false;
  71.                     setRect(pictRect, 0, 0, 19, 10);
  72.                 end;
  73.     end;
  74.  
  75.     procedure CalcEyeBall (num: integer);
  76.  
  77. {This lengthly procedure does not pertain to specific animation routines.  It merely sees at what step the specified}
  78. {eyeball is at, then changes the step, currentrect, and pictRect accordingly.}
  79. {One also very important thing this procedure does is set lastRect to the currentRect BEFORE the new currentRect is set.}
  80.  
  81.         procedure ChangeEye (num, newIn, left, top, right, bottom, mleft, mtop, mright, mbottom: integer);
  82.         begin
  83.             with eyeBallArray[num] do
  84.                 begin
  85.                     setRect(pictRect, left, top, right, bottom);
  86.                     moverect(currentRect, mleft, mtop, mright, mbottom);
  87.                     step := newIn;
  88.                 end;
  89.         end;
  90.  
  91.     begin
  92.         with eyeBallArray[num] do
  93.             begin
  94.                 lastRect := currentRect;            {Very important!!!!}
  95.                 case step of
  96.                     1: 
  97.                         if goingLeft then                {Determine which direction the eyeball is going.}
  98.                             ChangeEye(num, 20, 0, 11, 19, 23, 0, -2, 0, 0)
  99.                         else
  100.                             ChangeEye(num, 40, 0, 167, 19, 179, 0, -2, 0, 0);
  101.                     20: 
  102.                         ChangeEye(num, 21, 0, 24, 18, 38, 1, -2, 0, 0);
  103.  
  104. {The above for example does the following:}
  105. {If the step equals 20, then change the step to 21, change the PictRect to coordinates 0,24,18,38, and }
  106. {move the currentRect, one pixel over from the left and two pixels up from the top.}
  107.  
  108.                     21: 
  109.                         ChangeEye(num, 22, 0, 39, 16, 56, 2, -3, 0, 0);
  110.                     22: 
  111.                         ChangeEye(num, 23, 0, 57, 14, 75, 2, -1, 0, 0);
  112.                     23: 
  113.                         ChangeEye(num, 24, 0, 76, 12, 95, 2, -1, 0, 0);
  114.                     24: 
  115.                         ChangeEye(num, 25, 0, 96, 11, 115, 1, 0, 0, 0);
  116.                     25: 
  117.                         ChangeEye(num, 26, 0, 116, 14, 134, 0, 1, 3, 0);
  118.                     26: 
  119.                         ChangeEye(num, 27, 0, 135, 17, 151, 0, 2, 3, 0);
  120.                     27: 
  121.                         ChangeEye(num, 28, 0, 152, 18, 166, 0, 2, 1, 0);
  122.                     28: 
  123.                         ChangeEye(num, 29, 0, 167, 19, 179, 0, 2, 1, 0);
  124.                     29: 
  125.                         begin
  126.                             if currentRect.right > 300 then        {If the eyeball is at the edge of the screen, then change direction.}
  127.                                 begin
  128.                                     ChangeEye(num, 1, 0, 0, 19, 10, 0, 2, 0, 0);
  129.                                     goingLeft := false;
  130.                                 end
  131.                             else
  132.                                 begin
  133.                                     ChangeEye(num, 1, 0, 0, 19, 10, 0, 2, 0, 0);
  134.                                 end;
  135.                         end;
  136.                     40: 
  137.                         ChangeEye(num, 41, 0, 152, 18, 166, 0, -2, -1, 0);
  138.                     41: 
  139.                         ChangeEye(num, 42, 0, 135, 17, 151, 0, -2, -1, 0);
  140.                     42: 
  141.                         ChangeEye(num, 43, 0, 116, 14, 134, 0, -2, -3, 0);
  142.                     43: 
  143.                         ChangeEye(num, 44, 0, 96, 11, 115, 0, -1, -3, 0);
  144.                     44: 
  145.                         ChangeEye(num, 45, 0, 76, 12, 95, -1, 0, 0, 0);
  146.                     45: 
  147.                         ChangeEye(num, 46, 0, 57, 14, 75, -2, 1, 0, 0);
  148.                     46: 
  149.                         ChangeEye(num, 47, 0, 39, 16, 56, -2, 1, 0, 0);
  150.                     47: 
  151.                         ChangeEye(num, 48, 0, 24, 18, 38, -2, 3, 0, 0);
  152.                     48: 
  153.                         ChangeEye(num, 49, 0, 11, 19, 23, -1, 2, 0, 0);
  154.                     49: 
  155.                         begin
  156.                             if currentRect.left < 0 then      {If the eyeball is at the edge of the screen, then change direction.}
  157.                                 begin
  158.                                     ChangeEye(num, 1, 0, 0, 19, 10, 0, 2, 0, 0);
  159.                                     goingleft := true;
  160.                                 end
  161.                             else
  162.                                 begin
  163.                                     ChangeEye(num, 1, 0, 0, 19, 10, 0, 2, 0, 0);
  164.                                 end;
  165.                         end;
  166.                 end;
  167.             end;
  168.     end;
  169.  
  170.     procedure DoTheEyes;    {For each record in the eyeballarray, this procedure calls the above procedure, then}
  171.                                 {installs the eyeball into the Animation array with setAnimation.}
  172.         var
  173.             i: integer;
  174.     begin
  175.         for i := 1 to MaxEyes do
  176.             with eyeballarray[i] do
  177.                 begin
  178.                     CalcEyeBall(i);
  179.                     setAnimation(lastRect, currentRect, pictRect, eyeball, eyeballmask);  {eyeball and eyeball mask are the two}
  180.                                                                                            {ports that have the eyeball and mask pictures.}
  181.                 end;
  182.     end;
  183.  
  184.     function CenterWindow (width, height: integer): rect;
  185.  
  186. {This function centers a rect on the screen with dimensions of width and height.}
  187. {This function is called when the Mainwindow is created.}
  188.  
  189.         var
  190.             screenHeight, screenWidth, i: integer;
  191.     begin
  192.         with screenbits.bounds do
  193.             begin
  194.                 screenWidth := right - left;        {Get the exact pixel height and width of the screen.}
  195.                 screenHeight := bottom - top;
  196.             end;
  197.         tr[1].top := (screenHeight - height + 20) div 2;
  198.         tr[1].left := (screenWidth - width) div 2;
  199.         setRect(CenterWindow, tr[1].left, tr[1].top, tr[1].left + width, tr[1].top + height);
  200.     end;
  201.  
  202.     procedure SetUpWindow;
  203.  
  204.     begin
  205.         mainWindow := NewCWindow(nil, CenterWindow(300, 300), ' ', True, plainDBox, WindowPtr(-1), False, 0);    {Create the plain window at the center of the screen.}
  206.         setPort(mainWindow);                {Set the port to this new window.}
  207.         showWindow(mainWindow);            {Reveal the window.}
  208.     end;
  209.  
  210.  
  211.     procedure InitializePorts;
  212.     begin
  213.         DoColorPort(PureBack, 130, 300, 300);      {Create a color port with PICT ID 130 of dimensions 300 X 300}
  214.         DoColorPort(offLoad, -1, 300, 300);          {Create a blank color port of dimensions 300 X 300}
  215.         DoColorPort(EyeBall, 128, 19, 179);        {Create a color port with PICT ID 128 of dimensions 19 X 179}
  216.         DoPort(EyeBallmask, 129, 19, 179);        {Create a black and white port with PICT ID 129 of dimensions 19 X 179}
  217.     end;
  218.  
  219.     procedure DrawBackGround;      {This procedure copies the picture from port PureBack to the MainWindow.}
  220.     begin
  221.         copy(PureBack, MainWindow, PureBack^.portrect, MainWindow^.portrect);
  222.     end;
  223.  
  224.     procedure DrawMessage;
  225. {This procedure draws the message 'Click the Mouse to Quit the Demonstration' at coordinates 5,270}
  226. {This message is written on both the pureback port and the MainWindow so that the eyeballs will not erase the message.}
  227. {if the eyballs and the message happens to collide.}
  228.         var
  229.             fontnumber: integer;
  230.             thestring: str255;
  231.  
  232.         procedure WriteMessage (var TheGraf: Grafptr);
  233.  
  234.         begin
  235.             setPort(TheGraf);
  236.             GetFNum('Chicago', fontnumber);
  237.             textfont(fontnumber);
  238.             textsize(12);
  239.             moveto(5, 270);
  240.             drawstring('Click The Mouse To Quit The Demonstration.');
  241.         end;
  242.  
  243.     begin
  244.         WriteMessage(pureback);
  245.         WriteMessage(MainWindow);
  246.     end;
  247.  
  248. begin
  249.     getDateTime(randseed);    {Initialize the random number generator in the Macintosh system.}
  250.     hidecursor;                    {Hide the cursor.}
  251.     SetUpWindow;                {Create the window.}
  252.     InitializePorts;                {Create the ports.}
  253.     DrawBackGround;            {Draw the picture onto the window}
  254.     InitializeEyes;                {Initialize the variables for the eyeballs.}
  255.     setPort(MainWindow);        {Set the current port to the main window.}
  256.     DrawMessage;                {Draw the message onto the screen.}
  257.     while not button do        {While the mouse button is not down, perform the following loop.}
  258.         begin
  259.             InitAnimations;            {Reset the animations.}
  260.             DoTheEyes;                {Move the eyes and load them into the animation array.}
  261.             AnimateArray(pureback, offLoad, MainWindow);    {Animate the animation array.}
  262.                                                                 {The preserved background graphic is in the port pureback.}
  263.                                                                 {The port to be used for workspace is offLoad.}
  264.                                                                 {The port to draw the animations to is the window MainWindow.}
  265.         end;
  266.     showcursor;                {Show the cursor before exiting.}
  267. end.